function [maxtab, mintab]=APEpeakdet(datax, datay, delta, minmax)
% [maxtab, mintab]=APEpeakdet(datax, datay, delta, minmax)
%
% Detects peaks (minima) in datay and plots datax vs datay with annotated 
%  peaks (minima). Also puts out Vectors for peaks (minima). 
%
% INPUT     datax   :  dependend data or Index
%           datay   :  Signal
%           delta   :  detection threashold
%           minmax  :  sets detection;
%                       [1] = peaks (maxima only)
%                        2  = minima only
%                        3  = both
%
% OUTPUT    maxtab  :  [i-by-2] vector containing index and value of maxima
%           mintab  :  [i-by-2] vector containing index and value of minima
%
% EXAMPLE   APEpeakdet(ramandatax,smooth(ALSdatay),mean(ALSdatay),1);

%  (c) F. Dworkowski, 2011
%      Version 1.0, 2011-10-05

%Algorithm based on:  PEAKDET Detect peaks in a vector
%                      Eli Billauer, 3.4.05 (Explicitly not copyrighted).
%                      This function is released to the public domain; 
%                      Any use is allowed.

%% ===== INIT =====
maxtab = [];
mintab = [];

datay = datay(:); % Just in case this wasn't a proper vector
datax = datax(:);
  if length(datay)~= length(datax)
    errordlg('Input vectors datay and datax must have same length');
  end

% ----- check for correct delta -----
if (length(delta(:)))>1
  errordlg('Input argument DELTA must be a scalar');
end
if delta <= 0
  errordlg('Input argument DELTA must be positive');
end

% ----- check for minmax -----
if exist('minmax','var') == 0
    minmax = 1;         % Default
elseif minmax ~= [1 2 3]
    errordlg('minmax is not set correctly');
end

mn = Inf; mx = -Inf;
mnpos = NaN; mxpos = NaN;

%% ===== CODE =====
lookformax = 1;

for i=1:length(datay)
  this = datay(i);
  if this > mx, mx = this; mxpos = datax(i); end
  if this < mn, mn = this; mnpos = datax(i); end
  
  if lookformax
    if this < mx-delta
      maxtab = [maxtab ; mxpos mx];
      mn = this; mnpos = datax(i);
      lookformax = 0;
    end  
  else
    if this > mn+delta
      mintab = [mintab ; mnpos mn];
      mx = this; mxpos = datax(i);
      lookformax = 1;
    end
  end
end

%% ===== PLOT =====
figure(32)
plot(datax,datay)
hold on
switch minmax
    case 1
        if isempty(maxtab)
            errordlg('maxtab is empty!!');
            return
        else
            scatter(maxtab(:,1),maxtab(:,2),'r');
            text(maxtab(:,1),maxtab(:,2),num2str(round(maxtab(:,1))), ...
                'HorizontalAlignment','left', ...
                'VerticalAlignment', 'bottom', ...
                'Color',[1 0 0])
        end
    case 2
        if isempty(mintab)
            errordlg('mintab is empty!!');
        else
            scatter(mintab(:,1),mintab(:,2),'g');
            text(mintab(:,1),mintab(:,2),num2str(round(mintab(:,1))), ...
                'HorizontalAlignment','left', ...
                'VerticalAlignment', 'top', ...
                'Color',[0 0.5 0])
        end
    case 3
        if isempty(maxtab)
            errordlg('maxtab is empty!!');
            return
        else
            scatter(maxtab(:,1),maxtab(:,2),'r');
            text(maxtab(:,1),maxtab(:,2),num2str(round(maxtab(:,1))), ...
                'HorizontalAlignment','left', ...
                'VerticalAlignment', 'bottom', ...
                'Color',[1 0 0])
        end
        if isempty(mintab)
            errordlg('mintab is empty!!');
        else
            scatter(mintab(:,1),mintab(:,2),'g');
            text(mintab(:,1),mintab(:,2),num2str(round(mintab(:,1))), ...
                'HorizontalAlignment','left', ...
                'VerticalAlignment', 'top', ...
                'Color',[0 0.5 0])
        end
end
hold off